﻿using Commons.DomainBase;
using Commons.DomainBase.Models;
using Kogel.Dapper.Extension.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zack.Commons;

namespace IdentityService.Domain
{
    [Display(Rename = "users")]
    public class User : IHasCreationTime,ISoftDelete,IHasDeletionTime, IEntity
    {
        /// <summary>
        /// ID
        /// </summary>
        [Display(Rename = "id")]
        public long Id { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        [Display(Rename = "name")]
        public  string Name { get; set; }

        /// <summary>
        /// 登陆名
        /// </summary>
        [Display(Rename = "user_name")]
        public required string UserName { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [Display(Rename = "creation_time")]
        public DateTime CreationTime { get; set; }= DateTime.Now;

        /// <summary>
        /// 创建者ID
        /// </summary>
        [Display(Rename = "create_user_id")]
        public long CreateUserId { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Display(Rename = "password")]
        public  string? Password { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Display(Rename = "md5_password")]
        public  string? Md5Password { get; set; }
        /// <summary>
        /// 安全删除
        /// </summary>
        [Display(Rename = "is_deleted")]
        public int IsDeleted { get; set; } = 0;

        /// <summary>
        /// 删除时间
        /// </summary>
        [Display(Rename = "deletion_time")]
        public DateTime? DeletionTime  { get; set; }

        /// <summary>
        /// 创建对象
        /// </summary>
        /// <returns></returns>
        public static User Create( string userName,string password)
        {
            User item = new()
            {
                Id = IdWorkerHelper.NextId(),
                UserName = userName,
                Password= password,
                Md5Password= HashHelper.ComputeMd5Hash(password),
            };
            return item;
        }

        public void SoftDelete()
        {
            this.IsDeleted = 1;
            this.DeletionTime = DateTime.Now;
        }
    }
}
